'use strict'

const Service = require('egg').Service

class ReportService extends Service {
  async getReportData(type) {
    // console.log(typeId, 'typeId')

    const typeObj = {
      1: 'SELECT COUNT(c_staff.staffname) as staff_name , c_department.departname FROM `c_staff`,c_department WHERE c_staff.department_id=c_department.id  GROUP BY c_staff.department_id',
      2: 'SELECT SUM(c_device.device_count) as device_num , c_department.departname FROM `c_device`,c_department WHERE c_device.device_department_id=c_department.id  GROUP BY c_device.device_department_id',
    }

    const TYPEOBJ = {
      STATIC_STAFF_DEPARTMENT: {
        name: '员工部门分布统计',
        sql:
          'SELECT COUNT(c_staff.staffname) as staff_num , c_department.departname FROM `c_staff`,c_department WHERE c_staff.department_id=c_department.id  GROUP BY c_staff.department_id',
      },
      STATIC_DEVICE_DEPARTMENT: {
        name: '设备部门分布统计',
        sql:
          'SELECT SUM(c_device.device_count) as device_num , c_department.departname FROM `c_device`,c_department WHERE c_device.device_department_id=c_department.id  GROUP BY c_device.device_department_id',
      },
    }

    let result = await this.app.mysql.query(TYPEOBJ[type].sql)

    return this.ctx.getRESTfulData(
      {
        name: TYPEOBJ[type].name,
        result,
      },
      '获取数据成功',
      200
    )
  }
}

module.exports = ReportService
